import json
from django.http import HttpResponse, JsonResponse
from User.models import User
def sign(request):
    if request.method == "POST":
        body = json.loads(request.body)
        if len(User.objects.filter(mobile=body["mobile"])) != 0:
            return HttpResponse("用户已存在")
        User.objects.create(
            name=body["name"],
            mobile=body["mobile"],
            password=body["password"],
            age=body["age"],
            gender=body["gender"],
        ).save()
        if len(User.objects.filter(mobile=body["mobile"])) != 0:
            return HttpResponse("创建成功")
        else:
            return HttpResponse("创建失败")
def query(request):
    u=User.objects.filter(mobile=request.GET.get("mobile"))
    if len(u)==0:
        return HttpResponse("not found")
    return JsonResponse({
        'name': u[0].name,
        'mobile': u[0].mobile,
        'password': u[0].password,
        'age': u[0].age,
        'gender': u[0].gender,
        'isAdmin': u[0].isAdmin
    })
def login(request):
    if request.method == 'GET':
        user = User.objects.filter(mobile=request.GET.get("mobile"), password=request.GET.get("password"))
        if len(user) != 0:
            return HttpResponse("验证成功")
        else:
            return HttpResponse("验证失败")
def queryAll(request):
    u=User.objects.all()
    return JsonResponse({"data": [{
        'name': each.name,
        'mobile': each.mobile,
        'password': each.password,
        'age': each.age,
        'gender': each.gender,
    } for each in u]
    })